<?php
namespace app\filters;
use app\config\Status;
use app\models\Interfaces;
use app\models\Part;
use app\models\PartInterface;
use Yii;
use yii\base\ActionFilter;
class ApiFilter extends ActionFilter {
    public function beforeAction($action)
    {
        $response = Yii::$app->response;
        $response->format = \yii\web\Response::FORMAT_JSON;
        $role_id = Yii::$app->user->identity->part_id;
        if ($role_id !== 1) {
            $api = $action->getUniqueId();
            $api = ltrim($api,'v1/');
            $auth = Interfaces::find()->where(['auth' => $api])->select('part_ids')->One();
            if (empty($auth)) {
                $response->data = [
                    'bCode' => Status::AUTH,
                    'message' => '该角色还没分配权限',
                ];
                return false;
            } else {
                $ok = stripos($auth['part_ids'], "$role_id");

                if (!$ok) {
                    $response->data = [
                        'bCode' => Status::ALOT,
                        'message' => '你没有该权限',
                    ];
                    return false;
                }
            }

        }
        return parent::beforeAction($action);
    }
}